
$(document).ready(function() {

        //log('editAttributes.js: document.ready')

        // Ukoliko uredjujemo objekat, kroz ucitani html dosupne su promjenjljive:        
        // var editObjectDialog_object_type = '<?=$object['type']?>';
        // var editObjectDialog_owner = '<?=$object['owners_ID']?>';
        // var editObjectDialog_post_ID = '<?=$object['ID']?>';

        addAttributeDialog = function(url)
        {
            var url = url;
            var dialogClass = 'newAttributeDialog';
            var dialogTitle = 'Select attributes';
            var formToSubmit = '#attrToObjectDialog_form';
            var buttons = [
                {
                    id: 'newAttributeButton',
                    text: "Add",
                    click: function() { jQuery(formToSubmit).trigger('submit'); }
                }
            ];            
            openNewDialogOnOpen = function() {

                // Override generation of error label
                jQuery(formToSubmit).validate({

                        rules: {
                            attrToObjectDialog_attributes_ID: { check_item_dropdown: true } 
                        }, 
                        highlight: function(element) {
                            $(element).parent().parent().addClass("error");
                        },
                        unhighlight: function(element) {
                            $(element).parent().parent().removeClass("error");
                        },
                        submitHandler: function(form) {

                            //disable add button                            
                            $('#'+buttons[0].id).button("option", "disabled", true);

                            $('.'+dialogClass+' .status_info').html(loader_html());


                            var form = $(formToSubmit);
                            var form_data = form.serialize();

                            $.ajax({
                                    url: form.attr('action'),
                                    dataType: 'json',
                                    type: "POST",
                                    data: form_data,
                                    success: function (data, textStatus, xhr) {

                                        if(data.action==true){

                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'success'));

                                            // Fill form  with updated fileds
                                            get_object_updated_fields_helper();

                                            //log(myObject[0])

                                            function action_delay(){
                                                $('.'+dialogClass+' .status_info').html('Close dialog');
                                                $('.'+dialogClass).dialog("close");  
                                            }
                                            window.setTimeout(function() { action_delay(); }, 1001);

                                        }else{

                                            $('#'+buttons[0].id).button("option", "disabled", false);
                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'error'));
                                        }
                                    },
                                    error: function (xhr, textStatus, errorThrown) {
                                        //enable add button                            
                                        $('#'+buttons[0].id).button("option", "disabled", false); 
                                        $('.'+dialogClass+' .status_info').html(alert_html('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n','error'));
                                    }
                            });

                        },
                        errorPlacement: function(error, element){}

                })

                //log(myObject[0])

            }
            var DialogOnOpen = openNewDialogOnOpen;

            newDialog(url,dialogClass,dialogTitle,buttons,DialogOnOpen,567,410)
        }

        //DONE: Edit attribute don't display correct custom attribute on IE / we set cache: false on ajax request
        editAttributesDialog = function(url)
        {
            var url = url;
            var dialogClass = 'editAttributesDialog';
            var dialogTitle = 'Edit Attributes';
            var formToSubmit = '#editAttrToObjectDialog_form';
            var buttons = null;            
            openNewDialogOnOpen = function() {

                // Return a helper with preserved width of cells
                var fixHelper = function(e, ui) {
                    ui.children().each(function() {
                            $(this).width($(this).width());
                    });
                    return ui; 
                };

                //sort object attributes
                $("#sortable tbody").sortable({
                        helper: fixHelper,
                        placeholder: "ui-state-highlight",
                        axis: 'y',
                        handle: ".move_post",
                        update: function(event,ui)
                        {

                            $.ajax({
                                    url: $(formToSubmit).attr('action'),
                                    dataType: 'json',
                                    type: "POST",                              
                                    data: $(formToSubmit).serialize(),
                                    success: function (data, textStatus, xhr) {

                                        // Fill form  with updated fileds
                                        get_object_updated_fields_helper();

                                    },
                                    error: function (xhr, textStatus, errorThrown) {
                                        alert('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n');
                                    }
                            });

                        }
                }).disableSelection();

                // delete custom object attribute
                if(myObject[0]['hasDeleteHandler']==null)
                {
                    myObject[0]['hasDeleteHandler'] = true;
                    $('.delete_object_attribute').live('click',function(){

                        var conf = confirm("Delete attribute "+$(this).attr('rel')+" ?")
                        if (!conf){
                            return false;
                        }

                        var elem = this;

                        $.ajax({
                                url: base_url+'object_attributes/delete/'+$(this).attr('rel'),
                                dataType: 'json',
                                type: "POST",                              
                                success: function (data, textStatus, xhr) {

                                    $(elem).parent().parent().remove();

                                    // Fill form  with updated fileds
                                    get_object_updated_fields_helper();

                                },
                                error: function (xhr, textStatus, errorThrown) {
                                    alert('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n');
                                }
                        });

                })    
                }
                

                //log(myObject[0])

            }
            var DialogOnOpen = openNewDialogOnOpen;

            newDialog(url,dialogClass,dialogTitle,buttons,DialogOnOpen,1110,$(window).height()-40)
        }

})